/**
 * Copyright (C), 2015-2019, bogewang
 * FileName: RegisterController
 * Author:   foren236598164@aliyun.com
 * Date:     2019/9/18 14:14
 * Description: 注册控制层
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bogewang.member.controller;

import com.alibaba.fastjson.JSONObject;
import com.bogewang.core.base.BaseResponse;
import com.bogewang.core.bean.BeanTransferUtils;
import com.bogewang.member.controller.req.vo.RegisterVO;
import com.bogewang.member.entity.UserDTO;
import com.bogewang.member.feign.MemberRegisterServiceFeign;
import com.bogewang.web.base.BaseWebController;
import com.bogewang.web.utils.RandomValidateCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;

/**
 * 〈注册控制层〉
 *
 * @author foren236598164@aliyun.com
 * @create 2019/9/18
 * @since 1.0.0
 */
@Controller
public class RegisterController extends BaseWebController {
    private static final String MB_REGISTER_PAGE = "member/register";
    /**
     * 跳转到登陆页面页面
     */
    private static final String MB_LOGIN_FTL = "member/login";

    @Autowired
    private MemberRegisterServiceFeign memberRegisterServiceFeign;

    /**
     * 跳转到注册页面
     *
     * @return
     */
    @GetMapping("/register")
    public String getRegister() {
        return MB_REGISTER_PAGE;
    }

    /**
     * 跳转到注册页面
     *
     * @return
     */
    @PostMapping("/register")
    public String postRegister(@ModelAttribute("registerVO") @Validated RegisterVO registerVO, BindingResult bindingResult, Model model,
                               HttpSession session) {
        //1.接收表单参数,创建对象;
        if (bindingResult.hasErrors()){
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            setErrorMsg(model, errorMsg);
            return MB_REGISTER_PAGE;
        }
        //2.判断图形验证码是否正确;
        String graphicCode = registerVO.getGraphicCode();
        Boolean checkVerify = RandomValidateCodeUtil.checkVerify(graphicCode, session);
        if (!checkVerify){
            setErrorMsg(model, "图形验证码不正确!");
            return MB_REGISTER_PAGE;
        }
        //3.调用会员接口实现注册;
        UserDTO userDTO = BeanTransferUtils.EToT(registerVO, UserDTO.class);
        BaseResponse<JSONObject> register = memberRegisterServiceFeign.register(userDTO);
        if (!isSuccess(register)){
            setErrorMsg(model, register.getRtnMsg());
            return MB_REGISTER_PAGE;
        }

        //4.跳转登录页面;
        return MB_LOGIN_FTL;
    }

}